package com.ruoyi.common.utils;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Objects;

/**
 * The type Rest template util.
 *
 * @ClassName RestTemplate
 * @Description
 * @Author sizegang
 * @Date2024/11/30 00 :27
 */
public class RestTemplateUtil {
    /**
     * The Rest template.
     */
    static final RestTemplate restTemplate = new RestTemplate();

    static {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_HTML, MediaType.TEXT_PLAIN));
        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
    }


    /**
     * Get response entity.
     *
     * @param <T>    the type parameter
     * @param url    the url
     * @param tClass the t class
     * @return the response entity
     */
    public static <T> ResponseEntity<T> get(String url, Class<T> tClass) {
        return restTemplate.getForEntity(url, tClass);
    }


    public static <T> ResponseEntity<T> post(String url, Object params, Class<T> tClass) {
        return restTemplate.postForEntity(url, params, tClass);
    }
}
